<template>
	<view style="background: rgb(237, 245, 247);">
		<view class="top-container">

		</view>
		<view class="main-container" style="background: #ffffff;">
			<view style="width: 100%;height: 98%;;">
				<view class="main-div1">
					<text style="">学员姓名:{{item.memberUserName}}</text>
				</view>
				<!-- <view class="main-div1">
					<view class="main-float"><text>原体重:{{userInfo.memberUserWeight}}KG</text></view>
					<view class="main-float"><text>现体重:{{item.userWeight}}KG</text></view>


				</view>
				<view class="main-div1">
					<view class="main-float"><text>原体脂率:{{userInfo.memberUserBodyFat}}%</text></view>
					<view class="main-float"><text>现体脂率:{{item.userBfp}}%</text></view>
				</view> -->
				<view class="div1">
					<view style="width: 100%;height: 100%;;margin: 0 auto;">
						<view style="width: 100%;float: left;">
							<span style="color: rgba(46, 48, 76, 1);">学员案例：</span>
						</view>

						<view v-for="(group, time) in userInfoList" style="width: 100%;height: 47%;overflow: auto;">
							<view style="width: 100%;height: 12%;">
								<view style="width: 100%;height: 100%;float: left;">
									<span style="color: rgba(46, 48, 76, 1);">{{ formatDate(time) }}</span>
									<span style="color: rgba(46, 48, 76, 1);margin-left: 10%;">今日锻炼:请填写数据</span>
								</view>
							</view>
							<!--轮播图开始-->
							<swiper
								style="height: 88%;background: rgb(241, 241, 241);border-radius: 15rpx;"
								:autoplay="true" :current="currentIndex[time]" :circular="true" previous-margin="190rpx"
								next-margin="190rpx" :interval="3000" :duration="100" @change="(v)=>swierChange(v,time)">
								<swiper-item v-for="(item,i) in group" :key="i">
									<image 
									:class="currentIndex[time] === i?'active':''" style="width: 350rpx;"
									class="slide-image"
									:src="'http://101.43.223.224:8765/image/getImageById?id=' + item.actionImage"
									@click="previewImage(item.actionImage)">
									</image>
								</swiper-item>
							</swiper>
							<!--轮播图结束-->
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="card3-v2">
			<view style="width: 35%;height: 120rpx;margin: 0 auto;">
				<view class="bottom1" @click="fan()">
					<text>返回</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				item: {},
				fileList1: [],
				ImageFile: '',
				file1: [],
				ImageFile2: '',
				title: '',
				articleForm: {},
				userInfo: {},
				userInfoList: [],
				currentIndex: {},
			}
		},
		onLoad(option) {
			this.item = JSON.parse(option.item) //打印出上个页面传递的参数。
			this.selectUserInfo()
			this.selectUserInfoImage()

		},
		methods: {
			// swiper容器改变事件
			swierChange(e,time) {
				console.log(time)
				this.currentIndex[time] = e.detail.current
				console.log(this.currentIndex)
			},
			previewImage(item) {
				console.log(item)
				uni.previewImage({
					current: 'http://101.43.223.224:8765/image/getImageById?id=' + item,
					urls: ['http://101.43.223.224:8765/image/getImageById?id=' + item]
				});
			},
			// 查询用户案例图片
			selectUserInfoImage() {
				uni.request({
					url: this.URL + 'memberUser/selectUserInfoImage',
					method: 'GET',
					data: {
						coachId: uni.getStorageSync('teacherinfo').id,
						id: this.item.memberUserId,
					},
					success: (res) => {
						console.log(res)
						// 对返回的数据进行处理，将相同时间的图片放在一个子数组中
						this.userInfoList = this.groupImagesByTime(res.data.data.data);
					}
				})
			},
			// 处理数据，按时间分组
			groupImagesByTime(images) {
				const groupedImages = {};
				//  所有swiper 的对象   curridex 
				const naindex={};
				for (const image of images) {
					const createTime = this.formatDate(image.createTime);
					if (groupedImages[createTime]) {
						groupedImages[createTime].push(image);
					} else {
						naindex[createTime]= 0
						groupedImages[createTime] = [image];
					}
				}
				this.currentIndex=naindex
				return groupedImages;
			},
			formatDate(dateTime) {
				const date = new Date(dateTime);
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				return `${year}-${month}-${day}`;
			},
			// 查询用户信息
			selectUserInfo() {
				uni.request({
					url: this.URL + 'memberUser/selectUserCase',
					method: 'GET',
					data: {
						venueUniqueIdentifier: uni.getStorageSync('teacherinfo').venueUniqueIdentifier,
						id: this.item.memberUserId,
						coachId: uni.getStorageSync('teacherinfo').id
					},
					success: (res) => {
						this.userInfo = res.data.data.data
					}
				})
			},
			fan() {
				uni.navigateBack({
					delta: 1 // 返回的页面数，这里是返回上一页，所以是1
				});
			},
			uploadImg() {
				if (!this.ImageFile || this.ImageFile == '') {

					return
				}
				if (!this.ImageFile2 || this.ImageFile2 == '') {

				}
				if (!this.title || this.title == '') {

				}
				var ths = this;
				const formData = {
					...this.articleForm
				};
				uni.uploadFile({
					url: 'http://127.0.0.1:17000/UserRecord/uploadImg',
					filePath: this.ImageFile.url,
					name: 'file',
					formData: formData,
					header: {
						'content-type': 'multipart/form-data',
					},
					success: function(res) {
						// ths.img = res.data.data.data
						ths.item.caseStartPhoto = res.data

						uni.uploadFile({
							url: 'http://127.0.0.1:17000/UserRecord/uploadImg',
							filePath: ths.ImageFile2.url,
							name: 'file',
							formData: formData,
							header: {
								'content-type': 'multipart/form-data',
							},
							success: function(res) {
								// ths.img = res.data.data.data
								ths.item.caseEndingPhoto = res.data
								uni.request({
									url: 'http://127.0.0.1:17000/UserRecord/addCaseRecords',
									method: 'POST',
									data: {
										coachId: uni.getStorageSync('teacherinfo').id,
										memberId: ths.item.id,
										caseStartPhoto: ths.item.caseStartPhoto,
										caseEndingPhoto: ths.item.caseEndingPhoto,
										caseNotes: ths.title
									},
									success: function(res) {
										if (res.data.data.code == 200) {
											wx.switchTab({
												url: '/pages/home/anli',
												success: function(e) {
													var page =
														getCurrentPages()
														.pop();
													if (page ==
														undefined ||
														page == null)
														return;
													page.onLoad();
													// 添加消息提示
													uni.showToast({
														title: '操作成功',
														icon: 'success',
														duration: 2000
													});
												}
											});
										} else {
											uni.showToast({
												title: '操作失败',
												icon: 'error',
												duration: 2000
											});
										}
									},
									fail: function(error) {
										// 处理请求失败
										console.error('上传课程失败，请检查网络');
									}
								});

							},
							fail: function(error) {
								// 处理请求失败
								console.error('开启训练失败，请检查网络');
							}
						})

					},
					fail: function(error) {
						// 处理请求失败
						console.error('开启训练失败，请检查网络');
					}
				})
			},

			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				console.log(this[`fileList${event.name}`])
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					this.ImageFile = lists[i]

					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			deletePic2(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead2(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`file${event.name}`].length
				console.log(this[`file${event.name}`])
				lists.map((item) => {
					this[`file${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					this.ImageFile2 = lists[i]

					let item = this[`file${event.name}`][fileListLen]
					this[`file${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return
			},
		}
	}
</script>

<style>
	.slide-image {
		position: absolute;
		height: 300rpx;
		width: 570rpx;
		border-radius: 15rpx;
		z-index: 0;
		opacity: 0.7;
		top: 10%;
		margin: 0 -40rpx;
	}

	swiper {
		height: 430rpx;
	}

	.active {
		opacity: 1;
		z-index: 10;
		height: 450rpx;
		width: 600rpx;
		top: 2%;
		
	}

	.div1 {
		width: 100%;
		height: 95%;
		background: rgba(255, 255, 255, 1);
		overflow-y: auto;
		box-sizing: border-box;
	}

	.img1 {
		width: 100%;
		height: 100%;
	}

	.hd {
		overflow-x: auto;
		display: -webkit-box;
		-webkit-overflow-scrolling: touch;
	}

	.top-container {
		height: 5vh;
		width: 100%;
	}

	.main-container {
		letter-spacing: 2rpx;
		font-size: 28rpx;
		height: 85vh;
		width: 90%;
		padding: 20rpx;
		box-sizing: border-box;
		box-shadow: 0 2px 10px #dfdfdf;
		margin-bottom: 18px;

		margin: 0 auto;
	}

	.main-div1 {
		width: 100%;
		height: 50rpx;
	}

	.main-div3 {
		width: 100%;
		height: 100rpx;
	}

	.main-float {
		float: left;
		width: 50%;
		height: 30rpx;
	}

	.card3-v2 {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120rpx;
		box-shadow: 0 2px 10px #dfdfdf;
		border-radius: 40rpx 40rpx 0 0;
	}

	.bottom1 {
		width: 100%;
		height: 70rpx;
		float: left;
		margin-top: 25rpx;
		background-image: linear-gradient(to bottom right, #94A9FD, #9ECFFF, #b9e1ff);
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		border-radius: 20rpx;
	}
</style>